package com.cs.bd.ad.self;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.widget.LinearLayout;

import com.play.screen.livescreen.AdInfo;
import com.play.screen.livescreen.AppConnect;

/**
 * Created by Administrator on 2016/9/22.
 */
public class WapsAdsSdkHelper {

    private static String channel = "200";

    public static boolean sHasInitAds;

    public static void setChannel(String channel) {
        WapsAdsSdkHelper.channel = channel;
    }

    private static void closeCrashReport(Context activity) {
        try {
            getAppConnect(activity).setCrashReport(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static AppConnect getAppConnect(Context context) {
        try {
            return AppConnect.getInstance(AdIdHelper.getWapsAppId(context), channel, context);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean initPopAd(Context context) {
        try {
            getAppConnect(context).initPopAd(context);
            return true;
        } catch (Throwable e) {
            return false;
        }
    }

    public static boolean hasPopAd(Context context) {
        try {
            return getAppConnect(context).hasPopAd(context);
        } catch (Throwable e) {
            return false;
        }
    }

    public static boolean showPopAd(Context context) {
        try {
            getAppConnect(context).showPopAd(context);
            return true;
        } catch (Throwable e) {
            return false;
        }
    }

    public static boolean showAppOffers(Context context) {
        try {
            getAppConnect(context).showAppOffers(context);
            return true;
        } catch (Throwable e) {
            return false;
        }
    }

    /**
     * 预加载广告信息
     *
     * @param activity
     */
    public static void initAdInfo(Activity activity) {
        if (!sHasInitAds) {
            try {
                closeCrashReport(activity);
                getAppConnect(activity).initAdInfo();
                sHasInitAds = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取广告信息
     *
     * @param activity
     * @return
     */
    public static AdInfo getAdInfo(Context activity) {
        try {
            return getAppConnect(activity).getAdInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 广告单击
     *
     * @param activity
     * @param adId
     */
    public static void clickAd(Context activity, String adId) {
        try {
            getAppConnect(activity).clickAd(activity, adId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void downloadAd(Context activity, String adId) {
        try {
            getAppConnect(activity).downloadAd(activity, adId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showBannerAd(Context context, LinearLayout viewGroup) {
        getAppConnect(context).showBannerAd(context, viewGroup);
    }

    public static void showMiniAd(Context context, LinearLayout viewGroup) {
        getAppConnect(context).setAdBackColor(Color.TRANSPARENT);
        getAppConnect(context).showMiniAd(context, viewGroup, 10);
    }
}
